package ch18._03;

public class Solution {
    public static int[] subset(int m, int[] A) {
	int target[] = new int[m];
	int[] array = A.clone();
	for (int i = 0; i < m; i++) {
	    int index = i + (int)((A.length - i) * Math.random());
	    int tmp = array[index];
	    array[index] = array[i];
	    array[i] = tmp;
	    target[i] = tmp;
	}
	return target;
    }

    public static void init(int[] A) {
	if (A == null || A.length == 0)
	    return;
	for (int i = 0; i < A.length; i++) {
	    A[i] = i;
	}
    }

    public static void print(int A[]) {
	if (A == null || A.length == 0)
	    return;
	for (int i = 0; i < A.length; i++) {
	    if (i % 10 == 0)
		System.out.println();
	    System.out.print(A[i] + "\t");
	}
	System.out.println();
    }

    public static void main(String... args) {
	int n = 10;
	int m = n >> 1;
	int[] A = new int[n];
	init(A);
	print(A);
	int[] sub = subset(m, A);
	print(sub);
    }
}